<?php
require_once (__DIR__ . "/../inc/functions.php");
require_once (__DIR__ . "/Keywords.php");

class products
{
	static function GetAll()
	{
		$conn = GetConnection();
		$results = $conn->query('SELECT * FROM products');
		$error=$conn->error;
		return $results;
		
		
	}
	
	static function Get($id)
	{
		$conn = GetConnection();
		$results = $conn->query("SELECT * FROM products WHERE id=$id");
		$row = $results->fetch_assoc();
		$error=$conn->error;		
		return $row;
	}
	
	static function Exists($id)
	{
		$conn = GetConnection();
		$conn->query("SELECT * FROM products WHERE id=$id");
		$result = $conn->error == '';
		$conn->close();
		return $result; 
	}
	
	static function Insert($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = 	"INSERT INTO products (name,sku,shortDescription,longDescription,category,numInStock,location,thumb,image,price) "
			.	"Values ('$row2[name]','$row2[sku]','$row2[shortDescription]','$row2[longDescription]',$row2[category],$row2[numInStock],'$row2[location]','$row2[thumb]','$row2[image]','$row2[price]'); ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Update($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = 	"UPDATE products "
			.	"Set name='$row2[name]',sku='$row2[sku]',shortDescription='$row2[shortDescription]',longDescription='$row2[longDescription]',category=$row2[category],numInStock=$row2[numInStock],location='$row2[location]',thumb='$row2[thumb]',image='$row2[image]',price='$row2[price]' "
			.	"WHERE id=$row2[id] ";
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Delete($id)
	{
		$conn = GetConnection();
		$sql = 	"DELETE FROM products "
			.	"WHERE id=$id ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;				
	}
	static function Validate($row)
	{
		$results=array();
		if(empty($row['name'])) $results['name'] = 'Name is required';		
		if(empty($row['sku'])) $results['sku'] = 'SKU is required';		
		if(empty($row['shortDescription'])) $results['shortDescription'] = 'Short Description is required';		
		if(empty($row['longDescription'])) $results['longDescription'] = 'Long Description is required';		
		if(empty($row['numInStock'])) $results['numInStock'] = 'Num In Stock is required';	
		if(empty($row['location'])) $results['location'] = 'Location is required';		
		if(empty($row['thumb'])) $results['thumb'] = 'Thumb is required';	
		if(empty($row['image'])) $results['image'] = 'Image is required';
		if(empty($row['price'])) $results['price'] = 'Price is required';
		return count($results) > 0 ? $results : true;
	}
	
}

?>
